﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using iTextSharp;
using iTextSharp.text.pdf;
using iTextSharp.text.pdf.parser;

namespace Hec.TextMining
{
    public class PdfToTxtConverter
    {
        //Traduce all pdf in folder and subfolders
        public string Source { get; private set; }
        public string Destination { get; private set; }
        public PdfToTxtConverter(string source, string destination)
        {
            var currentAppPath = AppDomain.CurrentDomain.BaseDirectory;
            currentAppPath = currentAppPath.Replace(@"\Tests\bin\Debug", "");

            Source = currentAppPath + source;
            Destination = currentAppPath + destination;
        }

        public void GetListOfFilesPerDirectory()
        {
            var directoryInfo = new DirectoryInfo(Source);
            var directories = directoryInfo.GetDirectories();

            directories.Select(x =>
            {
                return x;
            }).ToList();

        }

        public void ConvertToSingleTextFileWithCategory()
        {
            var directoryInfo = new DirectoryInfo(Source);

            var allTexts = "Category" + '\t' + "FileName" + '\t' + "Text" + '\n';

            directoryInfo.GetDirectories()
                            .SelectMany(directories => directories.GetFiles())
                            .Select(file =>
                            {
                                if(!file.Name.StartsWith("_category"))
                                {
                                    throw new Exception("no category associated with file, can't run this method");
                                }
                                var category = file.Name.Substring(0, 11);
                                var newName = file.Name.Replace(category, "");

                                var pdfReader = new PdfReader(file.FullName);
                                var text = string.Empty;

                                for (int page = 1; page <= pdfReader.NumberOfPages; page++)
                                {
                                    try
                                    {
                                        ITextExtractionStrategy strategy = new SimpleTextExtractionStrategy();
                                        //ITextExtractionStrategy strategy = new SimpleTextExtractionStrategy();
                                        string currentText = PdfTextExtractor.GetTextFromPage(pdfReader, page, strategy);
                                        currentText = Encoding.UTF8.GetString(ASCIIEncoding.Convert(Encoding.Default, Encoding.UTF8, Encoding.Default.GetBytes(currentText)));
                                        currentText = currentText.ToLowerInvariant().Replace("\r", "").Replace("\n", "").Replace("\t", "");
                                        text += currentText;
                                    }
                                    catch { }

                                }
                                var test = text.ToString().ToArray().Distinct().ToList();
                                allTexts+=category+"  " + '\t' + newName +"  "+ '\t' + text +"  "+ " \n";

                                return file;
                            })
                            .ToList();
            File.WriteAllText(Destination + @"\" + "AllTexts.txt", allTexts.ToString());

        }

        public void ConvertToTextFiles()
        {
            var directoryInfo = new DirectoryInfo(Source);
            directoryInfo.GetDirectories()
                            .SelectMany(directories => directories.GetFiles())
                            .Select(file =>
                            {
                                var pdfReader = new PdfReader(file.FullName);
                                StringBuilder text = new StringBuilder();

                                for (int page = 1; page <= pdfReader.NumberOfPages; page++)
                                {
                                    try
                                    {
                                        ITextExtractionStrategy strategy = new SimpleTextExtractionStrategy();
                                        //ITextExtractionStrategy strategy = new SimpleTextExtractionStrategy();
                                        string currentText = PdfTextExtractor.GetTextFromPage(pdfReader, page, strategy);
                                        text.Append(System.Environment.NewLine);
                                        currentText = Encoding.UTF8.GetString(ASCIIEncoding.Convert(Encoding.Default, Encoding.UTF8, Encoding.Default.GetBytes(currentText)));
                                        text.Append(currentText.ToLowerInvariant());
                                    }
                                    catch { }

                                }

                                File.WriteAllText(Destination + @"\" + file.Name.Replace(".pdf", "") + ".txt", text.ToString());

                                return file;
                            })
                            .ToList();


        }


    }
}
